package com.work.service.main.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.work.constant.ExceptionDescription;
import com.work.constant.TallyType;
import com.work.dto.main.TShipBillStatusSearchDTO;
import com.work.entity.main.TShipBill;
import com.work.entity.main.TShipBillStatus;
import com.work.mapper.main.TShipBillStatusMapper;
import com.work.service.main.ITShipBillService;
import com.work.service.main.ITShipBillStatusService;
import com.work.vo.main.*;
import lombok.AllArgsConstructor;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.tool.utils.Func;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * 理货舱单状态接口实现
 *
 * @author ：dujianyu
 * @since ：Created in 2024/3/14 8:48
 */
@Service
@AllArgsConstructor
public class TShipBillStatusServiceImpl extends BaseServiceImpl<TShipBillStatusMapper, TShipBillStatus> implements ITShipBillStatusService {

    private ITShipBillService itShipBillService;

    @Override
    public boolean addTshipBillStatus(Long tShipBillId, Long transportShipId, Long transportBillId) {
        if (isTshipBillStatusExist(tShipBillId)) {
            return true;
        }

        TShipBillStatus entity = new TShipBillStatus();
        entity.setTShipBillId(tShipBillId);
        entity.setTransportShipId(transportShipId);
        entity.setTransportBillId(transportBillId);

        TShipBill detailByTransportBillId = itShipBillService.getDetailByTransportBillId(transportBillId);
        entity.setAllPiece(detailByTransportBillId.getPiece());
        entity.setAllTon(detailByTransportBillId.getTon());

        return super.saveOrUpdate(entity);
    }

    @Override
    public boolean changePieceTon(String tallyType, String addOrSub, Long tShipBillId, Integer piece, BigDecimal ton) {
        QueryWrapper<TShipBillStatus> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(TShipBillStatus::getTShipBillId, tShipBillId)
                .eq(TShipBillStatus::getIsDeleted, 0)
                .last("limit 1");
        TShipBillStatus tShipBillStatus = baseMapper.selectOne(queryWrapper);
        if (Func.isNull(tShipBillStatus)) {
            throw new RuntimeException(ExceptionDescription.DB_ERROR + "理货状态记录不存在");
        }
        switch (tallyType) {
            case TallyType.IN :
                return changeInPieceTon(tShipBillStatus, addOrSub, piece, ton);
            case TallyType.OUT :
                return changeOutPieceTon(tShipBillStatus, addOrSub, piece, ton);
            case TallyType.DIRECT :
                return changeDirectPieceTon(tShipBillStatus, addOrSub , piece, ton);
            case TallyType.TRANSFER_IN :
                return changeTransferInPieceTon(tShipBillStatus, addOrSub , piece, ton);
            case TallyType.TRANSFER_OUT :
                return changeTransferOutPieceTon(tShipBillStatus, addOrSub , piece, ton);
            case TallyType.RETREAT:
                return changeRetreatPieceTon(tShipBillStatus, addOrSub , piece, ton);
            default:
                throw new RuntimeException(ExceptionDescription.CONSTANT_ERROR + "理货方式不合法");
        }
    }

    @Override
    public IPage<ExpShipBillStatVO> shipExpBillStatPage(IPage<ExpShipBillStatVO> page, TShipBillStatusSearchDTO dto) {
        List<ExpShipBillStatVO> data = baseMapper.selectShipExpBillStatList(dto, page);
        return page.setRecords(data);
    }

    @Override
    public ExpShipBillStatTotalVO shipExpBillStatTotal(TShipBillStatusSearchDTO dto) {
        return baseMapper.selectShipExpBillStatTotal(dto);
    }

    @Override
    public IPage<ImpShipBillStatVO> shipImpBillStatPage(IPage<ImpShipBillStatVO> page, TShipBillStatusSearchDTO dto) {
        List<ImpShipBillStatVO> data = baseMapper.selectShipImpBillStatList(dto, page);
        return page.setRecords(data);
    }

    @Override
    public ImpShipBillStatTotalVO shipImpBillStatTotal(TShipBillStatusSearchDTO dto) {
        return baseMapper.selectShipImpBillStatTotal(dto);
    }

    @Override
    public TShipBillStatus updateStatus(TShipBillStatus entity) {
        if (Func.isNull(entity.getId())) {
            throw new RuntimeException(ExceptionDescription.NO_PARAM + "没有主键ID");
        }
//        TShipBillStatus data = baseMapper.selectById(entity.getId());
//        if (!Func.isNull(entity.getIsFinish())) {
//            data.setIsFinish(entity.getIsFinish());
//        }
//        if (!Func.isNull(entity.getShippingOrderStatus())) {
//            data.setShippingOrderStatus(entity.getShippingOrderStatus());
//        }

        if (!super.saveOrUpdate(entity)) {
            throw new RuntimeException(ExceptionDescription.DB_ERROR + "更新失败");
        }

        return entity;
    }

    @Override
    public CheckBillClearVO checkBillClear(Long transportBillId) {
        CheckBillClearVO data = baseMapper.selectCheckBillClear(transportBillId);
        return data;
    }

    @Override
    public IPage<ShipStatTallyVO> shipStatTallyPage(IPage<ShipStatTallyVO> page, TShipBillStatusSearchDTO dto) {
        List<ShipStatTallyVO> data = baseMapper.selectShipStatTallyList(dto, page);
        return page.setRecords(data);
    }

    private boolean changeInPieceTon(TShipBillStatus tShipBillStatus, String addOrSub, Integer piece, BigDecimal ton) {
        switch (addOrSub) {
            case TallyType.ADD :
                tShipBillStatus.setInPiece(tShipBillStatus.getInPiece() + piece);
                tShipBillStatus.setInTon(tShipBillStatus.getInTon().add(ton));
                break;
            case TallyType.SUB :
                tShipBillStatus.setInPiece(tShipBillStatus.getInPiece() - piece);
                tShipBillStatus.setInTon(tShipBillStatus.getInTon().subtract(ton));
                break;
            default:
                throw new RuntimeException(ExceptionDescription.CONSTANT_ERROR + "加减方式不合法");
        }
        return super.saveOrUpdate(tShipBillStatus);
    }

    private boolean changeOutPieceTon(TShipBillStatus tShipBillStatus, String addOrSub, Integer piece, BigDecimal ton) {
        switch (addOrSub) {
            case TallyType.ADD :
                tShipBillStatus.setOutPiece(tShipBillStatus.getOutPiece() + piece);
                tShipBillStatus.setOutTon(tShipBillStatus.getOutTon().add(ton));
                break;
            case TallyType.SUB :
                tShipBillStatus.setOutPiece(tShipBillStatus.getOutPiece() - piece);
                tShipBillStatus.setOutTon(tShipBillStatus.getOutTon().subtract(ton));
                break;
            default:
                throw new RuntimeException(ExceptionDescription.CONSTANT_ERROR + "加减方式不合法");
        }
        return super.saveOrUpdate(tShipBillStatus);
    }

    private boolean changeDirectPieceTon(TShipBillStatus tShipBillStatus, String addOrSub, Integer piece, BigDecimal ton) {
        switch (addOrSub) {
            case TallyType.ADD :
                tShipBillStatus.setDirectPiece(tShipBillStatus.getDirectPiece() + piece);
                tShipBillStatus.setDirectTon(tShipBillStatus.getDirectTon().add(ton));
                break;
            case TallyType.SUB :
                tShipBillStatus.setDirectPiece(tShipBillStatus.getDirectPiece() - piece);
                tShipBillStatus.setDirectTon(tShipBillStatus.getDirectTon().subtract(ton));
                break;
            default:
                throw new RuntimeException(ExceptionDescription.CONSTANT_ERROR + "加减方式不合法");
        }
        return super.saveOrUpdate(tShipBillStatus);
    }

    private boolean changeTransferInPieceTon(TShipBillStatus tShipBillStatus, String addOrSub, Integer piece, BigDecimal ton) {
        switch (addOrSub) {
            case TallyType.ADD :
                tShipBillStatus.setTransferInPiece(tShipBillStatus.getTransferInPiece() + piece);
                tShipBillStatus.setTransferInTon(tShipBillStatus.getTransferInTon().add(ton));
                break;
            case TallyType.SUB :
                tShipBillStatus.setTransferInPiece(tShipBillStatus.getTransferInPiece() - piece);
                tShipBillStatus.setTransferInTon(tShipBillStatus.getTransferInTon().subtract(ton));
                break;
            default:
                throw new RuntimeException(ExceptionDescription.CONSTANT_ERROR + "加减方式不合法");
        }
        return super.saveOrUpdate(tShipBillStatus);
    }

    private boolean changeTransferOutPieceTon(TShipBillStatus tShipBillStatus, String addOrSub, Integer piece, BigDecimal ton) {
        switch (addOrSub) {
            case TallyType.ADD :
                tShipBillStatus.setTransferOutPiece(tShipBillStatus.getTransferOutPiece() + piece);
                tShipBillStatus.setTransferOutTon(tShipBillStatus.getTransferOutTon().add(ton));
                break;
            case TallyType.SUB :
                tShipBillStatus.setTransferOutPiece(tShipBillStatus.getTransferOutPiece() - piece);
                tShipBillStatus.setTransferOutTon(tShipBillStatus.getTransferOutTon().subtract(ton));
                break;
            default:
                throw new RuntimeException(ExceptionDescription.CONSTANT_ERROR + "加减方式不合法");
        }
        return super.saveOrUpdate(tShipBillStatus);
    }

    private boolean changeRetreatPieceTon(TShipBillStatus tShipBillStatus, String addOrSub, Integer piece, BigDecimal ton) {
        switch (addOrSub) {
            case TallyType.ADD :
                tShipBillStatus.setRetreatPiece(tShipBillStatus.getRetreatPiece() + piece);
                tShipBillStatus.setRetreatTon(tShipBillStatus.getRetreatTon().add(ton));
                break;
            case TallyType.SUB :
                tShipBillStatus.setRetreatPiece(tShipBillStatus.getRetreatPiece() - piece);
                tShipBillStatus.setRetreatTon(tShipBillStatus.getRetreatTon().subtract(ton));
                break;
            default:
                throw new RuntimeException(ExceptionDescription.CONSTANT_ERROR + "加减方式不合法");
        }
        return super.saveOrUpdate(tShipBillStatus);
    }

    private boolean isTshipBillStatusExist(Long tShipBillId) {
        QueryWrapper<TShipBillStatus> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .select(TShipBillStatus::getId)
                .eq(TShipBillStatus::getIsDeleted, 0)
                .eq(TShipBillStatus::getTShipBillId, tShipBillId)
                .last("limit 1");
        TShipBillStatus tShipBillStatus = baseMapper.selectOne(queryWrapper);
        return !Func.isNull(tShipBillStatus);
    }
}
